/**
 * 
 */
package com.tuananh.radnom.gwt.client.overall;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.cell.core.client.SimpleSafeHtmlCell;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.TreeStore;
import com.sencha.gxt.examples.resources.client.model.BaseDto;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.menu.Item;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;
import com.sencha.gxt.widget.core.client.tree.Tree;
import com.tuananh.radnom.gwt.client.GreetingService;
import com.tuananh.radnom.gwt.client.GreetingServiceAsync;
import com.tuananh.radnom.gwt.shared.dto.MyBaseDTO;
import com.tuananh.radnom.gwt.shared.dto.NetNodeDTO;
import com.tuananh.radnom.gwt.shared.event.NetNodeTreeEvent;
import com.tuananh.radnom.gwt.shared.event.NetNodeTreeEventHandler;
import com.tuananh.radnom.gwt.shared.event.NetNodeTreeEvent.NodeTreeEventType;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;

/**
 * @author SilentSkyTA
 * 
 */
public class NetNodeTree implements IsWidget {
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	ArrayList<MyBaseDTO> listNode = new ArrayList<MyBaseDTO>();
	String type;
	public NetNodeTree(ArrayList<MyBaseDTO> listNode) {
		this.listNode = listNode;
	}

	public NetNodeTree() {
		// TODO Auto-generated constructor stub
	}

	public NetNodeTree(HandlerManager eventBus) {
		this.eventBus = eventBus;
	}

	interface TreeBundle extends ClientBundle {
		ImageResource plus();

		ImageResource minus();

		ImageResource folder();

		ImageResource folderOpen();
	}

	class KeyProvider implements ModelKeyProvider<MyBaseDTO> {

		public String getKey(MyBaseDTO item) {
			// TODO Auto-generated method stub
			return item.getKey();
		}

	}

	interface MyUiBinder extends UiBinder<Widget, NetNodeTree> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	FlowLayoutContainer con;

	@UiField(provided = true)
	TreeStore<MyBaseDTO> store = new TreeStore<MyBaseDTO>(new KeyProvider());

	@UiField
	Tree<MyBaseDTO, String> tree;
	HandlerManager eventBus = new HandlerManager(null);

	public Widget asWidget() {
		TreeBundle bundle = GWT.create(TreeBundle.class);

		if (store.getRootCount() < 1) {
			final MyBaseDTO root = new MyBaseDTO("NodeList", "Node List");
			store.add(root);
			if (listNode != null)
				for (MyBaseDTO dto : listNode) {
					dto.addToTreeStore(root, "", store);
				}
		}
		Widget widget = uiBinder.createAndBindUi(this);
		tree.setWidth(Window.getClientWidth() + "px");
		tree.setContextMenu(createContextMenu());
		con.setScrollMode(ScrollMode.AUTO);
		con.setBorders(true);

		tree.getStyle().setJointCloseIcon(bundle.plus());
		tree.getStyle().setJointOpenIcon(bundle.minus());
		tree.getStyle().setNodeCloseIcon(bundle.folder());
		tree.getStyle().setNodeOpenIcon(bundle.folderOpen());
		SimpleSafeHtmlCell<String> cell = new SimpleSafeHtmlCell<String>(
				SimpleSafeHtmlRenderer.getInstance(), "click") {
			@Override
			public void onBrowserEvent(Context context, Element parent,
					String value, NativeEvent event,
					ValueUpdater<String> valueUpdater) {
				super.onBrowserEvent(context, parent, value, event,
						valueUpdater);
				MyBaseDTO selectedItem = tree.getSelectionModel()
						.getSelectedItem();
				if (selectedItem !=null) {
					eventBus.fireEvent(new NetNodeTreeEvent(
							NodeTreeEventType.SELECT, null, null,
							selectedItem));
				}
			}
		};
		tree.setCell(cell);
		eventBus.addHandler(NetNodeTreeEvent.TYPE, handler);
		return widget;
	}

	NetNodeTreeEventHandler handler = new NetNodeTreeEventHandler() {

		public void onItemSelection(MyBaseDTO item) {
			// TODO Auto-generated method stub
			Info.display("Net Node Selection", "Selected Node Unique Name = "
					+ item.getValue());
		}

		public void onFetchNode(ArrayList<NetNodeDTO> listNode) {
			// TODO Auto-generated method stub
			updateStore(listNode);
		}

		public void onAddNode(NetNodeDTO node) {
			// TODO Auto-generated method stub

		}
	};

	public void updateStore(ArrayList<NetNodeDTO> listNode) {
		if (listNode != null) {
			store.clear();
			final MyBaseDTO root = new MyBaseDTO("NodeList", "Node List");
			store.add(root);
			for (MyBaseDTO dto : listNode) {
				dto.addToTreeStore(root, "", store);
			}
		}
	}

	@UiFactory
	public ValueProvider<MyBaseDTO, String> createValueProvider() {
		return new ValueProvider<MyBaseDTO, String>() {

			public String getValue(MyBaseDTO object) {
				return object.getValue();
			}

			public void setValue(MyBaseDTO object, String value) {
			}

			public String getPath() {
				return "value";
			}
		};
	}

	@UiHandler("expandAll")
	public void expandAll(SelectEvent event) {
		tree.expandAll();
	}

	@UiHandler("collapseAll")
	public void collapseAll(SelectEvent event) {
		tree.collapseAll();
	}

	Menu createContextMenu() {
		Menu menu = new Menu();
		// Edit menu
		MenuItem edit = new MenuItem("Edit");
		edit.addSelectionHandler(new SelectionHandler<Item>() {

			public void onSelection(SelectionEvent<Item> event) {
				// TODO Auto-generated method stub

			}
		});
		// Insert menu
		MenuItem insert = new MenuItem("Insert");
		insert.addSelectionHandler(new SelectionHandler<Item>() {

			public void onSelection(SelectionEvent<Item> event) {
				// TODO Auto-generated method stub

			}
		});
		// Delete menu
		MenuItem delete = new MenuItem("Delete");
		delete.addSelectionHandler(new SelectionHandler<Item>() {

			public void onSelection(SelectionEvent<Item> event) {
				// TODO Auto-generated method stub

			}
		});

		menu.add(insert);
		menu.add(edit);
		menu.add(delete);

		return menu;
	}
}
